#include "group.h"
#include "chat.h"
#include "ui_chat.h"

group::group(QString u_id, QString nid, QString nname,QString nmember_list, QPixmap npicture,
             QString nip,QString njob):
    user_id(u_id),
    user(nid,nname,nip,njob,npicture),
    member_list(nmember_list)
{

//     qDebug() << "???";
//    qDebug() << nid<<"nid";
    //chat = new ChatWidget(nid);


}

void group::get_member()
{

    //从本地数据库获得成员列表
    //并将其生成为small_meseage加入布局member_playout中

    //我直接从自己里读

    QStringList memberlist = member_list.split(",");

    for(auto m:memberlist){
        small_message* smg = new small_message(userMap[m]);
        smg->setMaximumSize(smg->size());
        smg->setMinimumSize(smg->size());
        member_playout->addWidget(smg);
    }
}

void group::show_member()
{
//    return ;
    QWidget *scrollareacontent;//滚动框的内容页面
    QScrollArea *scrollarea;//滚动框的区域
    QWidget* page = new QWidget(chat->ui->right_widget);//滚动框的总页面


    //page在page_friends平铺
    QVBoxLayout *m_layout = new QVBoxLayout(chat->ui->right_widget);
    m_layout->setContentsMargins(0, 0, 0, 0);
    m_layout->setSpacing(0);
    m_layout->addWidget(page);
    chat->ui->right_widget->setLayout(m_layout);//设置界面

    //设置带滚动区域和滚动内容界面
    scrollarea = new QScrollArea(page);
    scrollarea->setWidgetResizable(true);
    scrollarea->setGeometry(0,0,page->width(),page->height());
    scrollareacontent = new QWidget();
    scrollarea->setWidget(scrollareacontent);

    // 将 QScrollArea 放置在一个布局中，以便自动随窗口大小变化
    QVBoxLayout* scrollLayout = new QVBoxLayout();
    scrollLayout->addWidget(scrollarea);
    page->setLayout(scrollLayout);

    // member的垂直布局


    member_playout->setAlignment(Qt::AlignTop );//垂直靠上
    scrollareacontent->setLayout(member_playout);
    scrollareacontent->setStyleSheet("background-color: transparent;");

    member_playout->addStretch();
    member_playout->setSpacing(0);

}
void group::show_chat(){

    if(chat&&(!chat->visibleRegion().isEmpty())){
        qDebug() <<"弹出原群组聊天框";
        chat->activateWindow();
        chat->setWindowState((chat->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
        QScrollBar *scrollBar = chat->scrollarea->verticalScrollBar();
        scrollBar->setValue(scrollBar->maximum());
    }
    else{
        qDebug() <<"产生新群组聊天框";
        //chat_widget_list[id.toInt()]=&w;
        Friend recv,sender;

        if(!member_playout)
            delete member_playout;

        member_playout= new QVBoxLayout();


        get_member();
        recv.id = id.toInt();
        recv.ip = ip;
        recv.job = job;
        recv.name = name;

        sender.id=use_uid.toInt();
        sender.ip = use_ip;
        sender.job = userMap[use_uid]->job;
        sender.name = use_name;


        chat = new ChatWidget(sender,recv,userMap[use_uid]->picture,QPixmap(""));

            show_member();

        QVector<Message> recent_message = db.GetSomeRecentMessageByGroupid(id.toInt(), 20);
        qDebug() << recent_message.size();
        for(auto message=recent_message.rbegin();message !=recent_message.rend(); message++){
            qDebug() << "automessage";
            qDebug() << message->tid;
            qDebug() << user_id.toInt();
            if (message->tid == use_uid.toInt()){
                qDebug() << "In!";
                chat->sendMessage(*message);
            }
            else{
                qDebug() << "In2!";
                chat->receiveMessage(*message);
            }
        }

        //删除其中已有的布局
        if (chat->ui->right_widget->layout()) {
            // 判断布局类型是否为垂直布局
            if (QVBoxLayout *vLayout = qobject_cast<QVBoxLayout*>(chat->ui->right_widget->layout())) {
                // 移除所有部件并删除布局
                QLayoutItem *item;
                while ((item = vLayout->takeAt(0))) {
                    delete item->widget();
                    delete item;
                }
                delete vLayout;
            }
        }
        chat->ui->right_widget->setLayout(member_playout);

        //更改chat左右侧比例
        QLayout *currentLayout = chat->layout();
        if (chat->layout()) {
            // 判断布局类型是否为水平布局
            if (QHBoxLayout *hLayout = qobject_cast<QHBoxLayout*>(currentLayout)) {
                //               QLayoutItem *item;
                //               int i=0;
                //               while ((item = hLayout->takeAt(i))) {
                //                   qDebug()<<item->widget()->objectName();
                //                   i++;
                //               }
                // 获取 left_widget 和 right_widget 在布局中的索引
                int leftIndex = hLayout->indexOf(chat->ui->left_widget);
                int rightIndex = hLayout->indexOf(chat->ui->right_widget);

                // 设置缩放因子
                hLayout->setStretch(leftIndex, 618);
                hLayout->setStretch(rightIndex, 312);

                //设置布局属性
                hLayout->setContentsMargins(9, 9, 9, 9);
                hLayout->setSpacing(0);
                chat->setLayout(hLayout);
            }
        }

        chat->show();
        QScrollBar *scrollBar = chat->scrollarea->verticalScrollBar();
        scrollBar->setValue(scrollBar->maximum());
    }
}
